package com.lcj.aoch.rule.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcj.aoch.rule.config.ConstantConfiguration;
import com.lcj.aoch.rule.controller.form.DeleteRuleForm;
import com.lcj.aoch.rule.controller.form.InsertRuleForm;
import com.lcj.aoch.rule.controller.form.SearchRuleForm;
import com.lcj.aoch.rule.controller.form.UpdateRuleForm;
import com.lcj.aoch.rule.db.dao.RuleDao;
import com.lcj.aoch.rule.db.pojo.RuleEntity;
import com.lcj.aoch.rule.service.RuleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class RuleServiceImpl implements RuleService {

    @Resource
    private RuleDao ruleDao;

    @Resource
    private ConstantConfiguration constantConfiguration;

    @Override
    public PageInfo<HashMap> searchRule(SearchRuleForm form) {
        Map map = BeanUtil.beanToMap(form);
        map.put("key", constantConfiguration.getAesKey());
        PageHelper.startPage(form.getPageNum(), form.getPageSize());
        List<HashMap> list = ruleDao.searchRule(map);
        PageInfo<HashMap> pageInfo = new PageInfo(list);

        return pageInfo;
    }

    @Override
    public long insertRule(InsertRuleForm form) {
        form.setKey(constantConfiguration.getAesKey());
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(form);
        long rows = ruleDao.insertRule(stringObjectMap);
        return rows;
    }

    @Override
    public long updateRule(UpdateRuleForm form) {
        form.setKey(constantConfiguration.getAesKey());
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(form);
        long rows = ruleDao.updateRule(stringObjectMap);
        return rows;
    }

    @Override
    public long deleteRule(DeleteRuleForm form) {
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(form);
        long rows = ruleDao.deleteRule(stringObjectMap);
        return rows;
    }

    @Override
    public RuleEntity searchCurrentRule(SearchRuleForm form) {
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(form);
        RuleEntity rule = ruleDao.searchCurrentRule(stringObjectMap);
        return rule;
    }
}
